

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cai Shibo">
  <meta name="keywords" content="cc">
  
    <meta name="description" content="多表查询一对多（实体成员）符合面向对象思想：当所要查询的表内存在字段 关联 其他表的字段信息（如 id）时，该表对应的实体类中不应该只是一个字段属性，而是定义一个关联表的实体属性。 此时查询时会产生问题：因为该关联表的实体类内部存在属性信息，MyBatis 无法自动封装类成员内的信息，因此需要使用 resultMap 标签属性来声明。 Order.java 1234567891011public">
<meta property="og:type" content="article">
<meta property="og:title" content="SSM--D12">
<meta property="og:url" content="https://hahsx_xd.gitee.io/2022/05/02/SSM-D12/index.html">
<meta property="og:site_name" content="🍊CAI SHIBO🥬">
<meta property="og:description" content="多表查询一对多（实体成员）符合面向对象思想：当所要查询的表内存在字段 关联 其他表的字段信息（如 id）时，该表对应的实体类中不应该只是一个字段属性，而是定义一个关联表的实体属性。 此时查询时会产生问题：因为该关联表的实体类内部存在属性信息，MyBatis 无法自动封装类成员内的信息，因此需要使用 resultMap 标签属性来声明。 Order.java 1234567891011public">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-05-02T12:59:34.000Z">
<meta property="article:modified_time" content="2022-11-29T07:14:46.501Z">
<meta property="article:author" content="Cai Shibo">
<meta property="article:tag" content="cc">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>SSM--D12 - 🍊CAI SHIBO🥬</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"hahsx_xd.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":".|","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":"#"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":5},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":true,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="🍊CAI SHIBO🥬" type="application/atom+xml">
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>^_^ 🥬 CC</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="SSM--D12">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      Cai Shibo
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-05-02 20:59" pubdate>
        2022年5月2日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      7.5k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      38 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">SSM--D12</h1>
            
              <p class="note note-info">
                
                  最后更新：23 天前
                
              </p>
            
            <div class="markdown-body">
              <h4 id="多表查询"><a href="#多表查询" class="headerlink" title="多表查询"></a>多表查询</h4><h5 id="一对多（实体成员）"><a href="#一对多（实体成员）" class="headerlink" title="一对多（实体成员）"></a>一对多（实体成员）</h5><p>符合面向对象思想：<br>当所要查询的表内存在字段 关联 其他表的字段信息（如 id）时，<strong>该表对应的实体类中不应该只是一个字段属性，而是定义一个关联表的实体属性</strong>。</p>
<p>此时查询时会产生问题：因为该关联表的实体类内部存在属性信息，MyBatis <strong>无法自动封装类成员</strong>内的信息，因此需要使用 resultMap 标签属性来声明。</p>
<p><em>Order.java</em></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Order</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> id;<br>    <span class="hljs-keyword">private</span> Date ordertime;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> total;<br>    <span class="hljs-comment">//表示当前订单属于哪个用户</span><br>    <span class="hljs-keyword">private</span> User user;<br>    <br>    <span class="hljs-comment">/*---------------------</span><br><span class="hljs-comment">    get、set、toString 方法</span><br><span class="hljs-comment">    ---------------------*/</span><br>&#125;<br></code></pre></div></td></tr></table></figure>

<p><strong>OrderMapper.xml（相关配置）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">resultMap</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;orderMap&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;order&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--手动指定字段与实体属性的映射关系</span><br><span class="hljs-comment">        colum：数据表的字段名称</span><br><span class="hljs-comment">        property：实体的属性名称</span><br><span class="hljs-comment">        id是一个特殊的result，表示主键</span><br><span class="hljs-comment">    --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;oid&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;id&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;ordertime&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;ordertime&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;total&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;total&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;uid&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;user.id&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;user.username&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;user.password&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;birthday&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;user.birthday&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resultMap</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;findAll&quot;</span> <span class="hljs-attr">resultMap</span>=<span class="hljs-string">&quot;orderMap&quot;</span>&gt;</span><br>    select *, o.id oid from orders o, user u where o.id = u.id<br><span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br></code></pre></div></td></tr></table></figure>

<p><strong>注意：property 实体属性的名称与接口方法返回声明的类的内部属性名称要一致！！！</strong><br>即user.xxx，user 必须同实体类 Order.java 中的 User 成员名相同，xxx（如 id ）必须同实体类 User.java 的成员名相同。</p>
<hr>
<h6 id="补充说明（association标签）："><a href="#补充说明（association标签）：" class="headerlink" title="补充说明（association标签）："></a>补充说明（association标签）：</h6><p>可以使用 association 标签来声明类内部的类成员，后续即可直接使用声明的名称来为类内部的类成员映射相关属性。</p>
<p><strong>OrderMapper.xml（相关配置）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--&lt;result column=&quot;username&quot; property=&quot;user.username&quot;/&gt;</span><br><span class="hljs-comment">&lt;result column=&quot;password&quot; property=&quot;user.password&quot;/&gt;</span><br><span class="hljs-comment">&lt;result column=&quot;birthday&quot; property=&quot;user.birthday&quot;/&gt;--&gt;</span><br><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">    property：当前实体（order）中的属性名称（private User user）</span><br><span class="hljs-comment">    javaType：当前实体（order）中的属性的类型（User）</span><br><span class="hljs-comment">--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">association</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">javaType</span>=<span class="hljs-string">&quot;com.domain.User&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;uid&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;id&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;username&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;password&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;birthday&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;birthday&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">association</span>&gt;</span><br></code></pre></div></td></tr></table></figure>

<p><strong>注意：外部的 property 一定要与所要映射的类路径相同；内部的 property 实体属性的名称与接口方法返回声明的类的内部属性名称要一致！！！</strong><br>即外部的 user 与 实体类 Order.java 中的 User 成员名相同， javaType 是实体类 User.java 的路径，内部 property 名必须与实体类 User.java 的成员属性相同。</p>
<hr>
<h5 id="一对多（实体集合成员）"><a href="#一对多（实体集合成员）" class="headerlink" title="一对多（实体集合成员）"></a>一对多（实体集合成员）</h5><p>模拟一个业务场景：当一个用户存在多个订单时，如何查询到用户及其名下的所有订单。<br><strong>不能直接使用一个集合存放订单 id，不符合面向对象的思想；使用集合存放订单实体类</strong>。</p>
<p><strong>UserMapper.xml（相关配置）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">resultMap</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userMap&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;user&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;uid&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;id&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;username&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;password&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;birthday&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;birthday&quot;</span>/&gt;</span><br>    <span class="hljs-comment">&lt;!--配置集合信息</span><br><span class="hljs-comment">        property：集合名称</span><br><span class="hljs-comment">        ofType：当前集合中的数据类型</span><br><span class="hljs-comment">    --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">collection</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;orderList&quot;</span> <span class="hljs-attr">ofType</span>=<span class="hljs-string">&quot;order&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--封装order的数据--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;oid&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;id&quot;</span>/&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;ordertime&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;ordertime&quot;</span>/&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;total&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;total&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">collection</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resultMap</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;findAll&quot;</span> <span class="hljs-attr">resultMap</span>=<span class="hljs-string">&quot;userMap&quot;</span>&gt;</span><br>    select *, o.id oid from user u, orders o where u.id = o.id<br><span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br></code></pre></div></td></tr></table></figure>

<hr>
<h5 id="多对多"><a href="#多对多" class="headerlink" title="多对多"></a>多对多</h5><p>一个角色拥有多个角色，数据库中存在用户表、角色表、关系表。<br>解决方案：</p>
<ol>
<li>创建关系表映射的实体类</li>
<li>在用户实体中添加关系实体类集合</li>
<li>实现用户角色关系查询</li>
</ol>
<p><strong>UserMapper.xml（相关配置）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">resultMap</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userAndRoleMap&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;user&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;userId&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;id&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;username&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;password&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">collection</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;roleList&quot;</span> <span class="hljs-attr">ofType</span>=<span class="hljs-string">&quot;com.domain.Role&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;roleId&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;id&quot;</span>/&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;roleName&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;rolename&quot;</span>/&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">id</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;roleDesc&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;roleDesc&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">collection</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resultMap</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;findAllUserAndRole&quot;</span> <span class="hljs-attr">resultMap</span>=<span class="hljs-string">&quot;userAndRoleMap&quot;</span>&gt;</span><br>    SELECT * FROM `user` u, sys_role sr, sys_user_role sur WHERE sur.userId = u.id and sur.roleId = sr.id;<br><span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br></code></pre></div></td></tr></table></figure>

<hr>
<h3 id="MyBatis-注解开发"><a href="#MyBatis-注解开发" class="headerlink" title="MyBatis 注解开发"></a>MyBatis 注解开发</h3><h4 id="常用注解"><a href="#常用注解" class="headerlink" title="常用注解"></a>常用注解</h4><ul>
<li>@Insert：实现新增</li>
<li>@Update：实现更新</li>
<li>@Delete：实现删除</li>
<li>@Select：实现查询</li>
<li>@Result：实现结果集封装</li>
<li>@Delete：可以与 @Result 一起使用，封装多个结果集</li>
<li>@One：实现一对一结果集封装</li>
<li>@Many：实现一对多结果集封装</li>
</ul>
<hr>
<h4 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h4><p><strong>使用注解来表示映射关系，不再需要映射文件</strong>。  </p>
<p><strong>sqlMapConfig.xml（相关配置）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--不需要映射文件，自然无需在加载--&gt;</span><br><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">&lt;mappers&gt;</span><br><span class="hljs-comment">    &lt;mapper resource=&quot;com/mapper/userMapper.xml&quot;/&gt;</span><br><span class="hljs-comment">&lt;/mappers&gt;</span><br><span class="hljs-comment">--&gt;</span><br><br><span class="hljs-comment">&lt;!--加载映射关系--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mappers</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--指定接口所在的包！！不能写成接口！！--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">package</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;com.mapper&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">mappers</span>&gt;</span><br></code></pre></div></td></tr></table></figure>

<hr>
<h4 id="注解的简单使用"><a href="#注解的简单使用" class="headerlink" title="注解的简单使用"></a>注解的简单使用</h4><figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UserMapper</span> </span>&#123;<br>    <br>    <span class="hljs-meta">@Insert(&quot;insert into user values(#&#123;id&#125;, #&#123;username&#125;, #&#123;password&#125;, #&#123;birthday&#125;)&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">(User user)</span></span>;<br>    <br>    <span class="hljs-meta">@Update(&quot;update user set password = #&#123;password&#125;, username = #&#123;username&#125; where id = #&#123;id&#125;&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">update</span><span class="hljs-params">(User user)</span></span>;<br>    <br>    <span class="hljs-meta">@Delete(&quot;delete from user where id = #&#123;id&#125;;&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">delete</span><span class="hljs-params">(<span class="hljs-keyword">int</span> id)</span></span>;<br>    <br>    <span class="hljs-meta">@Select(&quot;select * from user where id = #&#123;id&#125;;&quot;)</span><br>    <span class="hljs-function">User <span class="hljs-title">findById</span><span class="hljs-params">(<span class="hljs-keyword">int</span> id)</span></span>;<br>    <br>    <span class="hljs-meta">@Select(&quot;select * from user;&quot;)</span><br>    <span class="hljs-function">List&lt;User&gt; <span class="hljs-title">findAll</span><span class="hljs-params">()</span></span>;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<hr>
<h4 id="注解的复杂使用"><a href="#注解的复杂使用" class="headerlink" title="注解的复杂使用"></a>注解的复杂使用</h4><h5 id="一对一-法-1"><a href="#一对一-法-1" class="headerlink" title="一对一 . 法 1"></a>一对一 . 法 1</h5><p><strong>OrderMapper.java（注解对应方法）</strong></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-meta">@Select(&quot;select *, o.id oid from orders o, user u where o.uid = u.id&quot;)</span><br><span class="hljs-meta">@Results(&#123;</span><br><span class="hljs-meta">        @Result(column = &quot;oid&quot;, property = &quot;id&quot;),</span><br><span class="hljs-meta">        @Result(column = &quot;ordertime&quot;, property = &quot;ordertime&quot;),</span><br><span class="hljs-meta">        @Result(column = &quot;total&quot;, property = &quot;total&quot;),</span><br><span class="hljs-meta">        @Result(column = &quot;uid&quot;, property = &quot;user.id&quot;),</span><br><span class="hljs-meta">        @Result(column = &quot;username&quot;, property = &quot;user.username&quot;),</span><br><span class="hljs-meta">        @Result(column = &quot;password&quot;, property = &quot;user.password&quot;)</span><br><span class="hljs-meta">&#125;)</span><br><span class="hljs-function">List&lt;Order&gt; <span class="hljs-title">findAll</span><span class="hljs-params">()</span></span>;<br></code></pre></div></td></tr></table></figure>

<h5 id="一对一-法-2（解耦思想）"><a href="#一对一-法-2（解耦思想）" class="headerlink" title="一对一 . 法 2（解耦思想）"></a>一对一 . 法 2（解耦思想）</h5><p><strong>OrderMapper.java（注解对应方法）</strong></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-meta">@Select(&quot;select * from orders&quot;)</span><br><span class="hljs-meta">@Results(&#123;</span><br><span class="hljs-meta">        @Result(column = &quot;oid&quot;, property = &quot;id&quot;),</span><br><span class="hljs-meta">        @Result(column = &quot;ordertime&quot;, property = &quot;ordertime&quot;),</span><br><span class="hljs-meta">        @Result(column = &quot;total&quot;, property = &quot;total&quot;),</span><br><span class="hljs-meta">        @Result(</span><br><span class="hljs-meta">                property = &quot;user&quot;,      //要封装的属性名称</span><br><span class="hljs-meta">                column = &quot;uid&quot;,         //根据该字段去查询其他表的数据</span><br><span class="hljs-meta">                javaType = User.class,  //要封装的实体类型</span><br><span class="hljs-meta">                //select属性 指定查询接口的方法获得数据</span><br><span class="hljs-meta">                one = @One(select = &quot;com.mapper.UserMapper.findById&quot;)</span><br><span class="hljs-meta">        )</span><br><span class="hljs-meta">&#125;)</span><br><span class="hljs-function">List&lt;Order&gt; <span class="hljs-title">findAll</span><span class="hljs-params">()</span></span>;<br></code></pre></div></td></tr></table></figure>

<hr>
<h5 id="一对多"><a href="#一对多" class="headerlink" title="一对多"></a>一对多</h5><ol>
<li><p><strong>OrderMapper.java（相关方法）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml">@Select(&quot;select * from orders where uid = #&#123;uid&#125;&quot;)<br>List<span class="hljs-tag">&lt;<span class="hljs-name">Order</span>&gt;</span> findOrderById(int uid);<br></code></pre></div></td></tr></table></figure></li>
<li><p><strong>UserMapper.java（相关方法）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml">@Select(&quot;select * from user&quot;)<br>@Results(&#123;<br>        @Result(id = true, column = &quot;id&quot;, property = &quot;id&quot;),<br>        @Result(column = &quot;username&quot;, property = &quot;username&quot;),<br>        @Result(column = &quot;password&quot;, property = &quot;password&quot;),<br>        @Result(<br>                property = &quot;orderList&quot;,<br>                column = &quot;id&quot;,<br>	//此处 JavaType value 是集合类型，不同于 xml 的集合内部实体类型<br>                javaType = List.class,<br>                many = @Many(select = &quot;com.mapper.OrderMapper.findOrderById&quot;)<br>        )<br>&#125;)<br>List<span class="hljs-tag">&lt;<span class="hljs-name">User</span>&gt;</span> findUserAndOrder();<br></code></pre></div></td></tr></table></figure></li>
</ol>
<hr>
<h5 id="多对多-1"><a href="#多对多-1" class="headerlink" title="多对多"></a>多对多</h5><ol>
<li><p><strong>RoleMapper.java（相关方法）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml">@Select(&quot;select * from sys_user_role sur, sys_role sr where sur.roleId = sr.id and sur.userId = #&#123;uid&#125;&quot;)<br>List<span class="hljs-tag">&lt;<span class="hljs-name">Role</span>&gt;</span> findRoleByUid(int uid);<br></code></pre></div></td></tr></table></figure></li>
<li><p><strong>UserMapper.java（相关方法）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml">@Select(&quot;select * from user&quot;)<br>@Results(&#123;<br>        @Result(id = true, column = &quot;id&quot;, property = &quot;id&quot;),<br>        @Result(column = &quot;username&quot;, property = &quot;username&quot;),<br>        @Result(column = &quot;password&quot;, property = &quot;password&quot;),<br>        @Result(<br>                property = &quot;roleList&quot;,<br>                column = &quot;id&quot;,<br>                javaType = List.class,<br>                many = @Many(select = &quot;com.mapper.RoleMapper.findRoleByUid&quot;)<br>        )<br>&#125;)<br>List<span class="hljs-tag">&lt;<span class="hljs-name">User</span>&gt;</span> findUserAndRole();<br></code></pre></div></td></tr></table></figure></li>
</ol>
<p>与一对多不同点：调用的 findRoleByUid 查询中使用了多表查询。</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章未经允许，严禁转载！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/05/02/SSM-D11/">
                        <span class="hidden-mobile">SSM--D11</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     CC <i class="iconfont icon-love"></i> XX
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    
      <script  src="/js/img-lazyload.js" ></script>
    
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
